package cn.wgx.modules.sys.services;

import cn.wgx.modules.base.services.BaseService;
import cn.wgx.modules.sys.entity.*;
import cn.wgx.modules.sys.entity.form.FormMenuPer;
import cn.wgx.modules.sys.entity.mapper .SysMenuPermissionMapper;

import cn.wgx.modules.sys.util.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
public class SysMenuPermissionService extends BaseService<SysMenuPermission> {

    @Resource
    SysMenuPermissionMapper mapper;

    @Transactional(readOnly = true)
    public List<SysMenuPermission> getList(SysMenuPermission record, Boolean desc) {
		SysMenuPermissionExample example = new SysMenuPermissionExample();
		example.setOrderByClause("createDate " + (desc?"desc": "asc"));
		return mapper.selectByExample(example);
    }

    @Transactional(readOnly = true)
    public List<SysMenuPermission> getListByIds(List<Integer> ids) {
        if(ids == null || ids.isEmpty()){
            return emptyList;
        }
        List<Integer> uniList = getUniList(ids);
        if(!uniList.isEmpty()){
            SysMenuPermissionExample example = new SysMenuPermissionExample();
            SysMenuPermissionExample.Criteria criteria = example.createCriteria();
            criteria.andMenu_permission_idIn(uniList);
            List<SysMenuPermission> eduCourseAssignmentHis = mapper.selectByExample(example);
            return eduCourseAssignmentHis;
        }
        return null;
    }


    @Transactional(readOnly = true)
    public SysMenuPermission getOne(Integer id) {
		return mapper.selectByPrimaryKey(id);
    }

    @Transactional
    public SysMenuPermission insertOne( SysMenuPermission record) {
        mapper.insertSelective(record);
        return record;
    }


    @Transactional
    public SysMenuPermission updateOne( SysMenuPermission record) {
        mapper.updateByPrimaryKeySelective(record);
        return record;

    }


    @Transactional
    public int deleteOne( Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Transactional
    public int deleteByMenuId(Integer menu_id) {
        SysMenuPermissionExample example = new SysMenuPermissionExample();
        example.createCriteria().andMenu_idEqualTo(menu_id);
        return mapper.deleteByExample(example);
    }

    @Transactional
    public int updateMenuPermission( FormMenuPer formMenuPer) {
        SysMenuPermissionExample example = new SysMenuPermissionExample();
        example.createCriteria().andMenu_idEqualTo(formMenuPer.getMenu_id());
        int i = mapper.deleteByExample(example);
        SysUser sysUser = UserUtil.getUser();
        SysMenuPermission sysMenuPermission = new SysMenuPermission();
        sysMenuPermission.setMenu_id(formMenuPer.getMenu_id());
        if(sysUser != null){
            sysMenuPermission.setCreateUserId(sysUser.getUser_id());
            sysMenuPermission.setUpdateUserId(sysUser.getUser_id());
        }
        int r = 0;
        for(Integer per_id: formMenuPer.getPermission_ids()){
            sysMenuPermission.setPermission_id(per_id);
//            filter(sysMenuPermission, true);
            r += mapper.insertSelective(sysMenuPermission);
        }
        return r;

    }

    public void filter(SysMenuPermission sysMenuPermission, boolean insert){
        if(insert){

        }else{

        }
        super.filter(sysMenuPermission,insert);

    }
}